#include "browsewindow.h"
#include "ylib.h"
#include "searchwindow.h"

#include <stdio.h>
#include <stdbool.h>

#define y_ABS(X) ((X) > 0 ? (X) : -(X))
#define y_MAX(X, Y) ((X) > (Y) ? (X) : (Y))

#define MAX_BTN_COUNT     3 
#define BTN_P_POS_Y      40
#define BTN_WIDTH       100
#define BTN_HEIGHT      100
#define BTN_N_POS_Y     (480 - BTN_P_POS_Y - BTN_HEIGHT)
#define BTN_PIXELS      18
#define BTN_POS_X       700
#define BMP_POS_X       0
#define BMP_POS_Y       0
#define BMP_WIDTH       (800 - BTN_WIDTH)
#define BMP_HEIGHT      480
#define BMP_SLIDE_OFFSET       50
#define HINT_POS_Y       2

extern struct BDir bmpDir;
static list_node *curr_p = NULL;
static list_node *__phead = NULL;
static font *__fp = NULL;
static GraphRect __btns[MAX_BTN_COUNT];

static void __draw_bmp(list_node *p);
static void __draw_btns();
static void __draw_hint();
static void __choose_bmp(int k);
static int __bmp_slide(int x1, int y1, int x2, int y2);
static int __bmp_btn(int x, int y);

// 初始化 浏览界面
void init_browsewindow()
{
    // 当前图片  路径指针
    curr_p = bmpDir.node.next;

    // 字体指针
    __fp = fontLoad("../font/simfang.ttf");
    if (__fp == NULL){
        fprintf(stderr, "file:%s :%d __fp == NULL\n", __FILE__, __LINE__);
    }

    // 头节点
    __phead = &(bmpDir.node);
}

// 绘制 浏览界面
void draw_browsewindow()
{
    // 清屏 白色
    clear_lcd(0xffffff);

// fprintf(stderr, "file:%s :%d\n", __FILE__, __LINE__);

    // 绘制帮助信息
    __draw_hint();
    
// fprintf(stderr, "file:%s :%d\n", __FILE__, __LINE__);

    // 绘制 按钮
    __draw_btns();

// fprintf(stderr, "file:%s :%d\n", __FILE__, __LINE__);

    // 绘制当前图片
    __draw_bmp(curr_p);

// fprintf(stderr, "file:%s :%d\n", __FILE__, __LINE__);
}

// 检查按钮  以及滑动
int check_btn_BW(bool isReadFirst, int x1, int y1, int x2, int y2)
{
    // 图片区域
    if (isReadFirst && (x1 >= BMP_POS_X && x1<=BMP_POS_X+BMP_WIDTH )){

// fprintf(stderr, "slide x1:%d<%d\n", x1, BMP_POS_X+BMP_WIDTH);

        return __bmp_slide(x1, y1, x2, y2);
    }else { // 按钮区域

// fprintf(stderr, "btn\n");

        return __bmp_btn(x2, y2);
    }
}

// 销毁浏览界面
void destroy_browsewindow()
{
    fontUnload(__fp);
}

static void __draw_bmp(list_node *p)
{
    if (p == NULL || p == __phead) return ;

    // 清空 图片域
    clear_rect(0xffffff, BMP_POS_X, BMP_POS_Y, BMP_WIDTH, BMP_HEIGHT);

// fprintf(stderr, "file:%s :%d\n", __FILE__, __LINE__);

    Filename *fp = list_entry(p, typeof(*fp), node);
    ypic *bmpp = readBmp(fp->filename);

if (bmpp == NULL){
        fprintf(stderr, "file:%s :%d ybmp == NULL\n", __FILE__, __LINE__);
    }
// fprintf(stderr, "file:%s :%d  \nfilename:%s\n", __FILE__, __LINE__, fp->filename);

    // 绘制图片
    draw_graph(bmpp->data, BMP_POS_X, BMP_POS_Y, bmpp->width, bmpp->height, bmpp->bytesPixel, true);

// fprintf(stderr, "file:%s :%d\n", __FILE__, __LINE__);

    //释放
    destory_ypic(bmpp);

// fprintf(stderr, "file:%s :%d\n", __FILE__, __LINE__);
}

static void __draw_btns()
{
    // 绘制 上一张 按钮
    __btns[0] = create_Btn(BTN_POS_X, BTN_P_POS_Y, BTN_WIDTH, BTN_HEIGHT, __fp, BTN_PIXELS, 0xffffff, 0x0, "上一张");

    // 绘制 下一张 按钮
    __btns[1] = create_Btn(BTN_POS_X, BTN_N_POS_Y, BTN_WIDTH, BTN_HEIGHT, __fp, BTN_PIXELS, 0xffffff, 0x0, "下一张");

    // 绘制 退出 按钮
    __btns[2] = create_Btn(BTN_POS_X, (480/2)-(BTN_HEIGHT/2), BTN_WIDTH, BTN_HEIGHT, __fp, BTN_PIXELS, 0xffffff, 0x0, "退出");
}

static void __draw_hint()
{
    // 设置字体像素
    fontSetSize(__fp, BTN_PIXELS);
    // 获取画布
    bitmap *bmap = createBitmapWithInit(100, BTN_P_POS_Y, 3, 0xffffff<<8);
    // 写入字
    fontPrint(__fp, bmap, 2, 2, "尝试上下左", 0x0, 0);
    fontPrint(__fp, bmap, 2, BTN_P_POS_Y/2+2, "右滑动图片", 0x0, 0);
    // 打印画布
    draw_graph(bmap->map, BTN_POS_X, 2, bmap->width, bmap->height, bmap->byteperpixel, false);
    // 销毁画布
    destroyBitmap(bmap);
}

static void __choose_bmp(int k)
{
    if (k == -1){
        if (curr_p->prev == __phead){
            curr_p = curr_p->prev;
        }
        curr_p = curr_p->prev;
    }else if (k == 1){
        if (curr_p->next == __phead){
            curr_p = curr_p->next;
        }
        curr_p = curr_p->next;
    }
}

static int __bmp_slide(int x1, int y1, int x2, int y2)
{
    int ret = BWNot;
    int xoffset = x2 - x1;
    int yoffset = y2 - y1;
    int maxoffset = y_ABS(xoffset) > y_ABS(yoffset) ? xoffset : yoffset;

    if (y_ABS(maxoffset) < BMP_SLIDE_OFFSET)
        return ret;
    // 根据偏移量决定 显示上一张 还是下一张
    if (maxoffset > 0){
        __choose_bmp(1);
        ret = BWNext;
    }
    else{
        __choose_bmp(-1);
        ret = BWPrev;
    }
    
    __draw_bmp(curr_p);
    return ret;
}

static int __bmp_btn(int x, int y)
{
    for (int i=0; i<MAX_BTN_COUNT; ++i){
        if ((__btns[i].x <= x && __btns[i].x+__btns[i].width >= x) && (__btns[i].y <= y && __btns[i].y+__btns[i].height >= y)){

// printf("x = %d, x+width = %d, y = %d, y+height = %d\n", __btns[i].x <= x , __btns[i].x+__btns[i].width >= x , __btns[i].y <= y , __btns[i].y+__btns[i].height >= y);

            // 上一张
            if (i == BWPrev){
                __choose_bmp(-1);
                __draw_bmp(curr_p);
            }else if (i == BWNext){// 下一张
                __choose_bmp(1);
                __draw_bmp(curr_p);
            }
            return i;
        }
    }
    return BWNot;
}